package com.viewlibrary.loadview;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.RelativeLayout;

/**
 * 添加3个不同颜色的小圆，交换小圆位置并移动实现一个加载效果
 */
public class CircleLoadingView extends RelativeLayout {
    CircleView leftCircle,middleCircle,rightCircle;
    float translateDistance;//移动距离
    long duraingTime=500;
    boolean isStopAnimation=false;
    public CircleLoadingView(Context context) {
        this(context,null);
    }

    public CircleLoadingView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleLoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        translateDistance=dpToPx(30);
        setBackgroundColor(Color.WHITE);
        //添加三个小圆
        leftCircle=getCircleView();
        leftCircle.exChangeColor(Color.RED);
        middleCircle=getCircleView();
        middleCircle.exChangeColor(Color.BLUE);
        rightCircle=getCircleView();
        rightCircle.exChangeColor(Color.YELLOW);
        addView(leftCircle);
        addView(rightCircle);
        addView(middleCircle);
        //展开小圆
        post(new Runnable() {
            @Override
            public void run() {
                //展开动画
                expandAnimation();
            }
        });

    }

    /**
     * 展开
     */
    private void expandAnimation() {
        if (isStopAnimation)return;
        //左圆左移 中圆不动，右圆右移
         ObjectAnimator leftAnimator=ObjectAnimator.ofFloat(leftCircle,"translationX",0,-translateDistance);
        ObjectAnimator rightAnimator=ObjectAnimator.ofFloat(rightCircle,"translationX",0,translateDistance);

        AnimatorSet set=new AnimatorSet();
        set.setDuration(duraingTime);
        set.playTogether(leftAnimator, rightAnimator);
        set.setInterpolator(new DecelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
               //收缩
                shrinkAnimation();
            }
        });
        set.start();
    }

    /**
     * 收缩
     */
    private void shrinkAnimation() {
        if (isStopAnimation)return;
        ObjectAnimator leftAnimator=ObjectAnimator.ofFloat(leftCircle,"translationX",0,translateDistance);
        ObjectAnimator rightAnimator=ObjectAnimator.ofFloat(rightCircle,"translationX",0,-translateDistance);

        AnimatorSet set=new AnimatorSet();
        set.setDuration(duraingTime);
        set.playTogether(leftAnimator, rightAnimator);
        set.setInterpolator(new AccelerateInterpolator());
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                //小圆位置交换 左变中 中变右 右变左
                int leftColor = leftCircle.getCurrentColor();
                int middleColor=middleCircle.getCurrentColor();
                int rightColor=rightCircle.getCurrentColor();
                middleCircle.exChangeColor(leftColor);
                rightCircle.exChangeColor(middleColor);
                leftCircle.exChangeColor(rightColor);
                //展开
                expandAnimation();
            }
        });
        set.start();
    }

    private CircleView getCircleView(){
        CircleView circleView=new CircleView(getContext());
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(dpToPx(10),dpToPx(10));
        params.addRule(CENTER_IN_PARENT);
        circleView.setLayoutParams(params);
        return circleView;
    }

    private int dpToPx(int i) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,i,getResources().getDisplayMetrics());
    }

    @Override
    public void setVisibility(int visibility) {
        super.setVisibility(INVISIBLE);
        leftCircle.clearAnimation();
        rightCircle.clearAnimation();
        ViewGroup parent= (ViewGroup) getParent();
        if (parent!=null){
            parent.removeView(this);
            parent.removeAllViews();
        }
        isStopAnimation=true;
    }
}
